package com.pphh.demo;

import com.alibaba.ttl.TransmittableThreadLocal;
import com.alibaba.ttl.TtlRunnable;

import java.util.concurrent.*;

/**
 * Created by pphh on 2022/9/30.
 */
public class TestTtl {

    private ThreadLocal<String> threadLocal = new ThreadLocal<>();

    private InheritableThreadLocal<String> inheritableThreadLocal = new InheritableThreadLocal<>();

    private TransmittableThreadLocal<String> transmittableThreadLocal = new TransmittableThreadLocal<>();

    private ExecutorService pool = new ThreadPoolExecutor(5, 5, 1, TimeUnit.SECONDS, new LinkedBlockingQueue());

    public void test() throws InterruptedException {
        threadLocal.set("root0");
        inheritableThreadLocal.set("root1");
        transmittableThreadLocal.set("root2");
        transmittableThreadLocal.set("root3");

        CountDownLatch latch0 = new CountDownLatch(10);
        for (Integer i = 0; i < 10; i++) {
            int index = i;
            pool.submit(() -> {
                Logger.logMessage("子线程, value = " + threadLocal.get());
                threadLocal.set("R0-child-" + index);
                latch0.countDown();
                Logger.logMessage("子线程-after-set, value = " + threadLocal.get());
            });
        }
        latch0.await();
        Logger.logMessage("主线程, value = " + threadLocal.get());

        Logger.logMessage("--");
        CountDownLatch latch = new CountDownLatch(10);
        for (Integer i = 0; i < 10; i++) {
            int index = i;
            pool.submit(() -> {
                Logger.logMessage("子线程, value = " + inheritableThreadLocal.get());
                inheritableThreadLocal.set("R1-child-" + index);
                latch.countDown();
                Logger.logMessage("子线程-after-set, value = " + inheritableThreadLocal.get());
            });
        }
        latch.await();
        Logger.logMessage("主线程, value = " + inheritableThreadLocal.get());

        Logger.logMessage("--");
        CountDownLatch latch2 = new CountDownLatch(10);
        for (Integer i = 0; i < 10; i++) {
            int index = i;
            pool.submit(() -> {
                Logger.logMessage("子线程, value = " + transmittableThreadLocal.get());
                transmittableThreadLocal.set("R2-child-" + index);
                latch2.countDown();
                Logger.logMessage("子线程-after-set, value = " + transmittableThreadLocal.get());
            });
        }
        latch2.await();
        Logger.logMessage("主线程, value = " + transmittableThreadLocal.get());

        Logger.logMessage("--");
        CountDownLatch latch3 = new CountDownLatch(10);
        for (Integer i = 0; i < 10; i++) {
            int index = i;
            Runnable runnable = TtlRunnable.get(() -> {
                Logger.logMessage("子线程, value = " + transmittableThreadLocal.get());
                transmittableThreadLocal.set("R3-child-" + index);
                latch3.countDown();
                Logger.logMessage("子线程-after-set, value = " + transmittableThreadLocal.get());
            });
            pool.submit(runnable);
        }
        latch3.await();
        Logger.logMessage("主线程, value = " + transmittableThreadLocal.get());

        pool.shutdown();
        pool.awaitTermination(10, TimeUnit.SECONDS);
    }
}
